package tr.edu.ege.seagent.hobnobontology.instance;

import tr.edu.ege.seagent.hobnobontology.schema.HobNobVocabulary;

import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;

public class HobNobInstanceCreator {

	/**
	 * RDF model which is handled to create instance statements.
	 */
	private Model model;

	/**
	 * Creates a new instance of {@link HobNobInstanceCreator}.
	 * 
	 * @param model
	 *            RDF model to create instance statements into it.
	 */
	public HobNobInstanceCreator(Model model) {
		this.model = model;
	}

	/**
	 * Creates friend of member
	 * 
	 * @param memberURI
	 *            URI of member
	 * @param friendOfURI
	 *            URI of friend which is property
	 * @param otherMemberURI
	 *            URI of friend member
	 */
	public void createFriendOfMember(String memberURI, String friendOfURI,
			String otherMemberURI) {

		// Creates resource statement which has two of URIs have been given
		createResourceStatement(memberURI, friendOfURI, otherMemberURI);

	}

	/**
	 * This function provides us to the creation of resource.
	 * 
	 * @param resourceURI
	 *            URI of the class
	 * @param typeURI
	 *            URI of the type of the created resource
	 * 
	 */
	public void createInstance(String resourceURI, String typeURI) {

		Resource typeResource = model.createResource(typeURI);
		model.createResource(resourceURI, typeResource);

	}

	/**
	 * Creates the language literal.
	 * 
	 * @param resourceURI
	 *            URI of resource
	 * @param propertyURI
	 *            URI of property
	 * @param literal
	 *            language literal
	 */
	public void createLanguageLiteral(String resourceURI, String propertyURI,
			String literal) {
		Resource classResource = model.createResource(resourceURI);
		Property classProperty = model.createProperty(propertyURI);
		classResource.addLiteral(classProperty,
				model.createTypedLiteral(literal, XSDDatatype.XSDlanguage));

	}

	/**
	 * Creates the statement which include literals.
	 * 
	 * @param resourceURI
	 *            URI of resource
	 * @param propertyURI
	 *            URI of property
	 * @param literal
	 *            literal of given resource which can be string, float .. ,
	 *            object
	 * 
	 */
	public void createLiteralStatement(String resourceURI, String propertyURI,
			Object literal) {
		Resource classResource = model.createResource(resourceURI);
		Property classProperty = model.createProperty(propertyURI);
		classResource.addLiteral(classProperty,
				model.createTypedLiteral(literal));
	}

	/**
	 * 
	 * This function is provide us to adding member literals in our instance
	 * model
	 * 
	 * @param memberURI
	 *            URI of member that literals regard to him
	 * @param nameLiteral
	 *            name of member
	 * @param surnameLiteral
	 *            surname of member
	 * @param eMailLiteral
	 *            email of member
	 * @param phoneNumberLiteral
	 *            phone number of member
	 * @param languageLiteral
	 *            language that spoken by member
	 * @param gender
	 *            URI of gender class. It can be
	 *            {@link HobNobVocabulary#MALE_URI} or
	 *            {@link HobNobVocabulary#FEMALE_URI}.
	 * 
	 */
	public void createMember(String memberURI, String nameLiteral,
			String surnameLiteral, String eMailLiteral,
			String phoneNumberLiteral, String languageLiteral, String gender) {

		// Type of instance is being created...
		createInstance(memberURI, gender);

		// Name of member is being created...
		createLiteralStatement(memberURI, HobNobVocabulary.NAME_URI,
				nameLiteral);
		// Surname of member is being created...
		createLiteralStatement(memberURI, HobNobVocabulary.SURNAME_URI,
				surnameLiteral);
		// E-mail of member is being created...
		createLiteralStatement(memberURI, HobNobVocabulary.E_MAIL_URI,
				eMailLiteral);
		// Phone number of member is being created...
		createLiteralStatement(memberURI, HobNobVocabulary.PHONE_NUMBER_URI,
				phoneNumberLiteral);
		// The language used by member is being created...
		createLanguageLiteral(memberURI, HobNobVocabulary.LANGUAGE_URI,
				languageLiteral);

	}

	/**
	 * Creates resource statement with given resource*, property and resources.
	 * 
	 * resource* may be related with two or more resource so relatedResourceURIs
	 * is a string array.
	 * 
	 * @param resourceURI
	 *            * URI of resource
	 * @param propertyURI
	 *            URI of property
	 * @param relatedResourceURIs
	 *            URI of resource that related with given resource and property
	 */
	public void createResourceStatement(String resourceURI, String propertyURI,
			String... relatedResourceURIs) {
		// Class reSource and classProperty are created by given URIs
		Resource instanceResource = model.getResource(resourceURI);
		Property property = model.getProperty(propertyURI);

		// Adds properties to the classResource
		for (String relatedResourceURI : relatedResourceURIs) {
			Resource relatedResource = model.getResource(relatedResourceURI);
			instanceResource.addProperty(property, relatedResource);
		}
	}

}
